//
//  NSDictionary+JSON.m
//  UtilsDemo
//
//  Created by yunsheng on 15/4/22.
//  Copyright (c) 2015年 yunsheng. All rights reserved.
//

#import "NSDictionary+JSON.h"

#import "NSArray+JSON.h"

@implementation NSDictionary (JSON)

- (NSString *)toJsonStrCascade
{
    NSArray *valueArray = [self allValues];
    NSArray *keyArray = [self allKeys];
    NSString *jsonString = @"";
    for (int i=0; i<self.count; i++)
    {
        if ([valueArray[i] isKindOfClass:[NSString class]])
        {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":\"%@\"",jsonString,keyArray[i],valueArray[i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":\"%@\",",jsonString,keyArray[i],valueArray[i]];
            }
        }
        else if([valueArray[i] isKindOfClass:[NSDictionary class]] || [valueArray[i] isKindOfClass:[NSArray class]]) {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":%@",jsonString,keyArray[i],[valueArray[i] toJsonStrCascade]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":%@,",jsonString,keyArray[i],[valueArray[i] toJsonStrCascade]];
            }
        } else {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":%@",jsonString,keyArray[i],valueArray[i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\":%@,",jsonString,keyArray[i],valueArray[i]];
            }
        }
    }
    jsonString = [NSString stringWithFormat:@"{%@}",jsonString];
    
    return jsonString;
}

-(NSString *)toStrOfJsonStrCascade
{
    NSArray *valueArray = [self allValues];
    NSArray *keyArray = [self allKeys];
    NSString *jsonString = @"";
    for (int i=0; i<self.count; i++)
    {
        if ([valueArray[i] isKindOfClass:[NSString class]])
        {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":\\\"%@\\\"",jsonString,keyArray[i],valueArray[i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":\\\"%@\\\",",jsonString,keyArray[i],valueArray[i]];
            }
        }
        else if([valueArray[i] isKindOfClass:[NSDictionary class]] || [valueArray[i] isKindOfClass:[NSArray class]]) {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":%@",jsonString,keyArray[i],[valueArray[i] toStrOfJsonStrCascade]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":%@,",jsonString,keyArray[i],[valueArray[i] toStrOfJsonStrCascade]];
            }
        } else {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":%@",jsonString,keyArray[i],valueArray[i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\":%@,",jsonString,keyArray[i],valueArray[i]];
            }
        }
    }
    jsonString = [NSString stringWithFormat:@"{%@}",jsonString];
    
    return jsonString;
}

@end
